package com.briup.demo.Mapper;

import com.briup.demo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper

public interface UserMapper {

    public void deletedByIdXML(Integer id);



    //写sql语句 - 查询所有用户数据
    //@Select代表写的是select语句
    //在程序运行时框架会自动生成接口的实现类对象
    //并交给spring的IOC容器管理
    @Select("select * from t_user")
    public List<User> allUserList();
    //根据id查询用户
    //手动映射结果
//    @Results({@Result(column = "register_time",property = "registerTime")})
    @Select("select * from t_user where id =#{id}")
    public User getById(Integer id);
    //组合查询
    //查询姓张的同学，性别女，
    //注册时间 2024年11月25日10：45：50之后的，2024年11月26日10：46：42之前的
    //结果降序
    // 组合查询方法
//    @Select("select * from t_user " +
//            "where name like '%${name}%' " +
//            "and gender = ${gender} " +
//            "and register_time between #{beginTime} and #{endTime} " +
//            "order by register_time desc")
//    public List<User> test1
//            (@Param("name") String name,
//            @Param("gender") Short gender,
//            @Param("beginTime") LocalDateTime beginTime,
//            @Param("endTime") LocalDateTime endTime);

    @Select("select * from t_user " +
            "where name like '%${name}%' " +
            "and gender = ${gender} " +
            "and register_time between #{beginTime} and #{endTime} " +
            "order by register_time desc"
            )
            public List<User> getUserList
                    (@Param("name") String name,
                     @Param("gender") Short gender,
                     @Param("beginTime") LocalDateTime beginTime,
                     @Param("endTime") LocalDateTime endTime);
        
            //  @Select("SELECT * FROM t_user " +
//          "WHERE name LIKE CONCAT('%', #{name}, '%') " +
//          "AND gender = #{gender} " +
//          "AND register_time BETWEEN #{beginTime} AND #{endTime} " +
//          "ORDER BY register_time DESC"
//  )
//  List<User> getUserList(
//          @Param("name") String name,
//          @Param("gender") Short gender,
//          @Param("beginTime") LocalDateTime beginTime,
//          @Param("endTime") LocalDateTime endTime
//  );



    //根据id删除用户
    @Delete("delete from t_user where id = #{id}")
    public void deleteById(Integer id);
    //登录
    //无法防止sql注入
//    @Select("select  * from t_user where username=${username} and password=#{password} ")
    //可以预防sql注入
    @Select("select * from t_user where id=${id} and phone=${phone} ")
    public List<User> login(@Param("id") String id, @Param("phone") String phone);

    //新增
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into t_user (id,name,age,gender,phone,birthday,register_time) values (#{id},#{name},#{age},#{gender},#{phone},#{birthday},#{registerTime})")
    public void addUser(User user);

    //编辑
    @Update("update t_user set name=#{name},age=#{age},gender=#{gender},phone=#{phone} where id=#{id}")
//    @Update("update t_user set id=#{id} where name=#{name}")
    public void updateUser(User user);


}
